package com.yh.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yh.javabean.Permission;
import com.yh.javabean.Product;
import com.yh.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Auther: als
 * @Date: 2024/5/27 - 05 - 27 - 10:21
 * @Description: com.yh.controller
 * @version: 1.0
 */
@Controller
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    PermissionService permissionService;

    @RequestMapping("/findAll")
    public String findAll(@RequestParam(value = "page", required = false, defaultValue = "1") int page,
                          @RequestParam(value = "limit", required = false, defaultValue = "5") int limit,
                          @RequestParam(value = "permissionName", required = false, defaultValue = "") String permissionName,
                          HttpServletRequest request) {
         //设置分页查询的起始页面
        PageHelper.startPage(page, limit);
        //调用service层查询数据方法

        List<Permission> all = permissionService.findAll(permissionName);
        //创建一个PageInfo对象，用于存储分页信息，并将all列表作为参数传入
        PageInfo<Permission> pageInfo = new PageInfo<>(all);

        request.setAttribute("pageInfo",pageInfo);
        request.setAttribute("value", permissionName);
        return "/permission/permission-list";
    }

    @RequestMapping("/add")
    public String add(Permission permission){
        permissionService.addPermission(permission);
        return "redirect:findAll";
    }


    @RequestMapping("/deleteById")
    public String deleteById(int id){
        permissionService.delete(id);
        return "redirect:findAll";
    }

    @RequestMapping("/edit")
    public String edit(Permission permission){
        permissionService.editPermission(permission);
        return "redirect:findAll";
    }

    @RequestMapping("/findById")
    public String findById(int id,HttpServletRequest request){
        Permission permission = permissionService.findById(id);
        request.setAttribute("permission",permission);
        return "/permission/permission-edit";
    }


    //权限批量删除
    @RequestMapping("/deleteIds")
    @ResponseBody
    public String findById(@RequestBody List<Integer> ids){
        permissionService.deleteIds(ids);
        return "/permission/findAll";
    }
}
